﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Documents;
using localText = Proxy.Properties.Resources;

namespace HomagChina.CCT.ViewModels
{
    public class SettingsDirectionDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private InsertInfoHandle Handle_Insert;
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        private int _Id;
        private string _SourcePos;
        private int _Direction;
        private string _TargetPos;
        private List<T_BasicInfo> list_BasicInfo;

        //是否为编辑状态
        private bool _IsEdit;

        public string SourcePos
        {
            get => _SourcePos;
            set { _SourcePos = value; OnPropertyChanged(); }
        }

        public int Direction
        {
            get => _Direction;
            set { _Direction = value; OnPropertyChanged(); }
        }

        public int Id
        {
            get => _Id; set { _Id = value; OnPropertyChanged(); }
        }

        public string TargetPos
        {
            get => _TargetPos; set { _TargetPos = value; OnPropertyChanged(); }
        }

        public bool IsEdit
        {
            get => _IsEdit; set { _IsEdit = value; OnPropertyChanged(); }
        }

        //提交
        public RelayCommand ApplyCommand { get; }

        private ObservableCollection<string> directionOptions;

        public ObservableCollection<string> DirectionOptions
        {
            get => directionOptions;
            set { directionOptions = value; OnPropertyChanged(); }
        }

        private string _SelectedOption = string.Empty;

        public string SelectedOption
        {
            get => _SelectedOption;
            set
            {
                _SelectedOption = value;
                OnPropertyChanged();
            }
        }

        #endregion 属性

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="SelectItem"></param>
        /// <param name="IsEdit"></param>
        public SettingsDirectionDialogViewModel(SetttingDirectionModel SelectItem, Boolean IsEdit)
        {
            this.IsEdit = IsEdit;
            ApplyCommand = new RelayCommand(Apply);
            commonHandle = new CommonHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            list_BasicInfo = new List<T_BasicInfo>();
            InitDirectionOptions();
            list_BasicInfo = Handle_Search.GetBasicInfo();
            if (SelectItem != null)
            {
                this.Id = SelectItem.Id;
                this.SourcePos = SelectItem.SourcePos;
                this.TargetPos = SelectItem.TargetPos;
                var ii = list_BasicInfo.FirstOrDefault(s => s.Value.Equals(SelectItem.Direction.ToString())).Id;
                SelectedOption = DirectionOptions[ii - 1];
            }
        }

        /// <summary>
        /// 提交函数
        /// </summary>
        private void Apply()
        {
            //利用数据库查询获得基础信息
            Direction = int.Parse(list_BasicInfo.FirstOrDefault(s => s.Describe.Equals(SelectedOption)).Value);

            var t = new T_DirectionConfig
            {
                SourcePos = SourcePos,
                TargetPos = TargetPos,
                Direction = Direction
            };

            bool success = false;

            if (IsEdit)
            {
                success = Handle_Modify.ModifyDirectionInfo(t, this.Id);
            }
            else
            {
                success = Handle_Insert.InsertDirectionHandle(t);
            }

            if (success)
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Added_successfully);
            }
            else
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Add_Failure);
            }
            commonHandle.CloseDiaLog(localText.SettingsDirectionDialog);
        }

        private void InitDirectionOptions()
        {
            DirectionOptions = new ObservableCollection<string>
            {
                localText.Forward,
                localText.Turn_Left,
                localText.Turn_Right,
                localText.Forward_Left,
                localText.Forward_Right,
                localText.Forward_Left_Wide,
                localText.Forward_Right_Wide,
                localText.Forward_Long,
                localText.NG,
                localText.Off,
            };
        }
    }
}